﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                if (Session["stat"] == null) 
                {
                    PhoneBook.Manage.Stat.add("web");
                }
            }
        }

        protected void btn_login_Click(object sender, EventArgs e)
        {
            Hashtable param = new Hashtable();
            param["username"] = txb_username.Text;
            //param["userpwd"] = txb_userpwd.Text;
            Hashtable user = ERMap.ERMapHelper.getmodel("sys_users", param);
            #region 登录日志记录
            Hashtable logininfo = new Hashtable();
            logininfo["loginlogid"] = "0";
            logininfo["loginid"] = txb_username.Text;
            logininfo["usertype"] = "4";
            logininfo["userid"] = "0";
            logininfo["success"] = "登录成功";
            logininfo["localip"] = Commons.NetSocket.getAllIp();
            logininfo["logintime"] = DateTime.Now;
            //model["OutSysTime"] = "0";
            #endregion
            if (!txb_check.Text.Equals(Session["web_login_checkcode"]))
            {
                lt_msg.Text = "验证码错误";
                logininfo["success"] = "登录失败";
                logininfo["reason"] = lt_msg.Text;
                div_notify.Visible = true;
                PhoneBook.Manage.LoginLog.add(logininfo);
                return;
            }

            if (user == null)
            {
                lt_msg.Text = "用户不存在";
                logininfo["success"] = "登录失败";
                logininfo["reason"] = lt_msg.Text;
                div_notify.Visible = true;
                PhoneBook.Manage.LoginLog.add(logininfo);
                return;
            }
            else
            {
                logininfo["usertype"] = user["usertype"];
                logininfo["userid"] = user["userid"];
            }
            Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();
            //El6WuA4jUzKLbI7aQicoCkHD7B7PAxW14sni/iaQ9NMC2rS1WD1m8g==  初始密码123456
            if (!encrypt.ValidPwdBy3des(txb_userpwd.Text, Commons.StrUtil.FormatValue(user["userpwd"])))
            {
                lt_msg.Text = "密码错误";
                logininfo["success"] = "登录失败";
                logininfo["reason"] = lt_msg.Text;
                div_notify.Visible = true;
                PhoneBook.Manage.LoginLog.add(logininfo);
                return;
            }

            if (logininfo["success"].ToString() == "登录成功")
            {
                Session["userid"] = user["userid"];
                Session["username"] = user["username"];
                Session["nickname"] = user["nickname"];
                Session["usertype"] = user["usertype"];
                #region 
                logininfo["reason"] = "登录成功";
                PhoneBook.Manage.LoginLog.add(logininfo);
                #endregion
                Response.Redirect("~/index.aspx");
            }
        }
    }
}